package com.youfan.control;

import com.youfan.model.Order;
import com.youfan.service.impl.WeiXinPayService;
import com.youfan.service.impl.YinLianPayService;
import com.youfan.service.impl.ZhifuBaoPayService;
import com.youfan.service.weixin.PayRequest;
import com.youfan.service.weixin.PayResponse;
import com.youfan.service.weixin.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

@Controller
@Slf4j
public class PayControlout {
    @Autowired
    WeiXinPayService weiXinPayService;
    @Autowired
    YinLianPayService yinLianPayService;
    @Autowired
    ZhifuBaoPayService zhifuBaoPayService;

    /**
     *
     * @param paytype 1 微信支付 2 支付宝支付 3 银联卡支付
     * @param tradenumber
     * @param amount
     * @return 1支付成功 2支付失败
     */
    @RequestMapping(value = "payWithoutAmout")
    public int payWithAmout(String paytype,String tradenumber,int amount){
        int payresult = 2;
        if("1".equals(paytype)){
            payresult = weiXinPayService.payWithpayAmount(tradenumber,amount);
        }else if("2".equals(paytype)){
            payresult = yinLianPayService.payWithpayAmount(tradenumber,amount);
        }else if("3".equals(paytype)){
            payresult =zhifuBaoPayService.payWithpayAmount(tradenumber,amount);
        }
        return payresult;
    }


    @RequestMapping("/auth")
    public void auth(@RequestParam("code") String code) {
        log.info("进入auth方法。。。");
        log.info("code={}", code);

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx705762491229630b&secret=f984414914131383e75cd5741371618f&code=" + code + "&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        log.info("response={}", response);
        //待处理--开始
        String openid = "";
        Order order = new Order();
        //待处理--结束
        WxPayServiceImpl implent = new WxPayServiceImpl();
        PayRequest request = new PayRequest();
        request.setOpenid(openid);
        request.setOrderAmount(order.getPayamount());
        request.setOrderId(order.getId()+"");
        request.setOrderName("微信支付");
        PayResponse payResponse = implent.pay(request);
    }

    @RequestMapping("/notify")
    public PayResponse notify(String notifyData) {
        //1. 验证签名
        //2. 支付的状态
        //3. 支付金额
        //4. 支付人(下单人 == 支付人)
        WxPayServiceImpl wxPayServiceImpl = new WxPayServiceImpl();
        PayResponse payResponse = wxPayServiceImpl.asyncNotify(notifyData);

        //查询订单,payResponse.getOrderId()
        Order order = new Order();


        //修改订单的支付状态

        return payResponse;
    }
}
